package com.atguigu.utils;

import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.function.Consumer;

/**
 * @Author: shenhao
 * @CreatTime: 2024/10/27 12:33
 * @Version: 1.0
 * 高并发批处理下发通用工具类
 */
public class TaskBatchSendUtils {
    public static <T> void send(List<T> taskList, Executor threadPool, Consumer<? super T> consumer) throws InterruptedException {
        if (taskList == null || taskList.isEmpty()) return;
        if (Objects.isNull(consumer)){
            return;
        }
        CountDownLatch countDownLatch = new CountDownLatch(taskList.size());
        for (T conponOrShortMsg : taskList) {
            threadPool.execute(()->{
                try{
                    consumer.accept(conponOrShortMsg);
                } finally{
                    countDownLatch.countDown();
                }
            });
        }
        countDownLatch.await();
    }

    public static void disposeTask(String task){
        System.out.println(String.format("[%s]disposeTask下发优惠卷或短信成功",task));
    }

    public static void disposeTaskV2(String task){
        System.out.println(String.format("[%s]disposeTask下发邮件成功",task));
    }
}
